/**   
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-06 09:51:29  
 * Copyright (c) 2015- joysuch-版权所有     
 */
package com.joysuch.wwyt.bp.controller;

import javax.validation.Valid;

import com.joysuch.wwyt.bp.entity.BpRegulationDocument;
import com.joysuch.wwyt.bp.enums.RegulationDocumentTypes;
import com.joysuch.wwyt.bp.service.BpRegulationDocumentService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.risk.entity.RiskEmergencyCard;
import com.joysuch.wwyt.bp.entity.BpRegulationDocumentArchive;
import com.joysuch.wwyt.bp.service.BpRegulationDocumentArchiveService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 制度规范管理——文档归档
 */
@RestController
@Api(tags = "制度规范管理")
@RequestMapping("/bp/regulation-document-archive")
public class BpRegulationDocumentArchiveController {

    private static final Logger logger = LoggerFactory.getLogger(BpRegulationDocumentArchiveController.class);

    @Autowired
    private BpRegulationDocumentArchiveService bpRegulationDocumentArchiveService;

    @Autowired
    private BpRegulationDocumentService bpRegulationDocumentService;

    @RequestMapping("add")
    public Object add(@Valid BpRegulationDocumentArchive entity) {
        try {
            bpRegulationDocumentArchiveService.add(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("创建失败", e);
            return ResultBean.fail(101, "创建失败");
        }
    }

    @RequestMapping("update")
    public Object update(@Valid BpRegulationDocumentArchive entity) {
        try {
            if (entity.getId() == null) {
                return ResultBean.fail(102, "未指定主键");
            }
            bpRegulationDocumentArchiveService.update(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("更新失败：#"+entity.getId(), e);
            return ResultBean.fail(101, "更新失败");
        }
    }

    @RequestMapping("page")
    public Object page(Pageable page) {
        try {
            Page<BpRegulationDocumentArchive> data = bpRegulationDocumentArchiveService.findByPage(page);
            return ResultBean.pageData(data.getContent(), data.getTotalElements());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }

    @RequestMapping("delete")
    public Object delete(Long id) {
        try {
            bpRegulationDocumentArchiveService.delete(id);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("删除失败", e);
            return ResultBean.fail(101, "删除失败");
        }
    }

    @RequestMapping("archive")
    public Object archive(Long id) {
        try {

            BpRegulationDocument bpRegulationDocument = bpRegulationDocumentService.findById(id);
            if (bpRegulationDocument.getState() != RegulationDocumentTypes.AUDIT_PASS.getId()) {
                return ResultBean.fail(102, "文档未审核通过");
            }
            bpRegulationDocumentArchiveService.archive(id);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("重新编制失败", e);
            return ResultBean.fail(101, "重新编制失败");
        }
    }
}
